// FrontEnd Plus GUI for JAD
// DeCompiled : JHibernateTableSettingsPanel.class

package middlegen.plugins.hibernate.swing;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import middlegen.*;
import middlegen.plugins.hibernate.HibernateTable;
import middlegen.swing.BooleanNodeCheckBox;
import middlegen.swing.JTableSettingsPanel;

public class JHibernateTableSettingsPanel extends JTableSettingsPanel
{

    private final JLabel _beanKeyGeneratorLabel = new JLabel("  Key generator");
    private final JLabel _beanNameLabel = new JLabel("  Domain class name");
    private final JTextField _beanNameField = new JTextField();
    private final JLabel _schemaNameLabel = new JLabel("  Schema name");
    private final JTextField _schemaNameField = new JTextField();
    private final JLabel _persisterLabel = new JLabel("  Persister");
    private final JTextField _persisterField = new JTextField();
    private final JLabel _classDescriptionLabel = new JLabel("  Class description");
    private final JTextField _classDescriptionField = new JTextField();
    private final JLabel _extendsLabel = new JLabel("  Extends");
    private final JTextField _extendsField = new JTextField();
    private final BooleanNodeCheckBox _implementLifecycleCheckBox = new BooleanNodeCheckBox("Lifecycle interface", null);
    private final BooleanNodeCheckBox _implementValidatableCheckBox = new BooleanNodeCheckBox("Validatable interface", null);
    private final BooleanNodeCheckBox _pcKeyCheckBox = new BooleanNodeCheckBox("Plain Compoundkey", null);
    private final BooleanNodeCheckBox _proxyCheckBox = new BooleanNodeCheckBox("Enable proxies", null);
    private final BooleanNodeCheckBox _mutableCheckBox = new BooleanNodeCheckBox("Mutable", null);
    private final BooleanNodeCheckBox _dynamicUpdateCheckBox = new BooleanNodeCheckBox("Dynamic Update", null);
    private final BooleanNodeCheckBox _dynamicInsertCheckBox = new BooleanNodeCheckBox("Dynamic Insert", null);
    private final BooleanNodeCheckBox _selectBeforeUpdateCheckBox = new BooleanNodeCheckBox("Select Before Update", null);
    private final JRadioButton _publicRadioButton = new JRadioButton("public");
    private final JRadioButton _protectedRadioButton = new JRadioButton("protected");
    private final JRadioButton _privateRadioButton = new JRadioButton("private");
    private final JLabel _classScopeLabel = new JLabel("  Class scope");
    private final JLabel _implementsLabel = new JLabel("  Manage external class interfaces");
    private final JTextField _sourceTextField = new JTextField();
    private final DefaultComboBoxModel _implementsComboModel = new DefaultComboBoxModel();
    private final JComboBox _implementsComboBox;
    private final JComboBox _beanKeyGeneratorCombo = new JComboBox();
    private final JTextField _beanKeyGeneratorField = new JTextField();
    private final JLabel _batchSizeLabel = new JLabel("  Batch Size ");
    private final JTextField _batchSizeField = new JTextField();
    private final JLabel _whereLabel = new JLabel("     Where  ");
    private final JTextField _whereField = new JTextField();
    private final BooleanNodeCheckBox _implementEqualsHashcode = new BooleanNodeCheckBox("Generate Equals/Hash", null);
    private final DocumentListener _identityClassListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            updateTable(_beanNameField.getText());
        }

    };
    private final DocumentListener _schemaDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setDatabaseSchema(_schemaNameField.getText());
        }

    };
    private final DocumentListener _persisterDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setPersister(_persisterField.getText());
        }

    };
    private final DocumentListener _batchSizeDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setBatchSize(_batchSizeField.getText());
        }

    };
    private final DocumentListener _whereDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setWhere(_whereField.getText());
        }

    };
    private final DocumentListener _classDescriptionDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setClassDescription(_classDescriptionField.getText());
        }

    };
    private final DocumentListener _extendsDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setExtends(_extendsField.getText());
        }

    };
    private final DocumentListener _keyGeneratorArgListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            _currentTable.setKeyGeneratorArg(_beanKeyGeneratorField.getText());
        }

    };
    private final ActionListener toggleBtnMutableAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setMutable(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnProxyAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setProxy(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener togglePlainCompoundKeyAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setPlainCompoundKey(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnDynamicUpdateAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setDynamicUpdate(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnDynamicInsertAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setDynamicInsert(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnSelectBeforeUpdateAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setSelectBeforeUpdate(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnLifecycleAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setImplementLifecycle(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnEqualsHashcodeAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setEqualsHashcode(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener toggleBtnValidatableAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setImplementValidatable(((BooleanNodeCheckBox)e.getSource()).isSelected());
        }

    };
    private final ActionListener keyGeneratorComboAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            _currentTable.setKeyGenerator((String)_beanKeyGeneratorCombo.getSelectedItem());
        }

    };
    private HibernateTable _currentTable;

    public JHibernateTableSettingsPanel()
    {
        _implementsComboBox = new JComboBox(_implementsComboModel);
        setLayout(new BoxLayout(this, 0));
        JPanel firstGroup = new JPanel();
        firstGroup.setLayout(new BoxLayout(firstGroup, 1));
        firstGroup.setBorder(BorderFactory.createTitledBorder("Table Mapping Attributes"));
        JPanel secondGroup = new JPanel();
        secondGroup.setLayout(new BoxLayout(secondGroup, 1));
        secondGroup.setBorder(BorderFactory.createTitledBorder("Domain Class Meta Attributes"));
        JPanel firstSectionGroup1 = new JPanel();
        firstSectionGroup1.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel secondSectionGroup1 = new JPanel();
        secondSectionGroup1.setLayout(new GridLayout(0, 3, 4, 2));
        JPanel firstSectionGroup2 = new JPanel();
        firstSectionGroup2.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel secondSectionGroup2 = new JPanel();
        secondSectionGroup2.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel thirdSectionGroup2 = new JPanel();
        thirdSectionGroup2.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel fourthSectionGroup2 = new JPanel();
        fourthSectionGroup2.setLayout(new GridLayout(0, 3, 4, 2));
        firstSectionGroup1.add(_beanNameLabel);
        firstSectionGroup1.add(_beanNameField);
        firstSectionGroup1.add(_beanKeyGeneratorLabel);
        firstSectionGroup1.add(BuildKeyGeneratorPanel());
        firstSectionGroup1.add(_schemaNameLabel);
        firstSectionGroup1.add(_schemaNameField);
        firstSectionGroup1.add(_persisterLabel);
        firstSectionGroup1.add(_persisterField);
        firstSectionGroup1.add(BuildBatchSizePanel());
        firstSectionGroup1.add(BuildWherePanel());
        secondSectionGroup1.add(_pcKeyCheckBox);
        secondSectionGroup1.add(_proxyCheckBox);
        secondSectionGroup1.add(_mutableCheckBox);
        secondSectionGroup1.add(_dynamicUpdateCheckBox);
        secondSectionGroup1.add(_dynamicInsertCheckBox);
        secondSectionGroup1.add(_selectBeforeUpdateCheckBox);
        firstGroup.add(firstSectionGroup1);
        firstGroup.add(secondSectionGroup1);
        firstSectionGroup2.add(_classDescriptionLabel);
        firstSectionGroup2.add(_classDescriptionField);
        firstSectionGroup2.add(_extendsLabel);
        firstSectionGroup2.add(_extendsField);
        secondSectionGroup2.add(_classScopeLabel);
        secondSectionGroup2.add(BuildTableScopePanel());
        thirdSectionGroup2.add(_implementsLabel);
        thirdSectionGroup2.add(BuildImplementsPanel());
        fourthSectionGroup2.add(_implementLifecycleCheckBox);
        fourthSectionGroup2.add(_implementValidatableCheckBox);
        fourthSectionGroup2.add(_implementEqualsHashcode);
        secondGroup.add(firstSectionGroup2);
        secondGroup.add(secondSectionGroup2);
        secondGroup.add(thirdSectionGroup2);
        secondGroup.add(fourthSectionGroup2);
        add(firstGroup);
        add(secondGroup);
        _schemaNameField.getDocument().addDocumentListener(_schemaDocumentListener);
        _persisterField.getDocument().addDocumentListener(_persisterDocumentListener);
        _classDescriptionField.getDocument().addDocumentListener(_classDescriptionDocumentListener);
        _extendsField.getDocument().addDocumentListener(_extendsDocumentListener);
        _beanKeyGeneratorField.getDocument().addDocumentListener(_keyGeneratorArgListener);
        _batchSizeField.getDocument().addDocumentListener(_batchSizeDocumentListener);
        _whereField.getDocument().addDocumentListener(_whereDocumentListener);
        _pcKeyCheckBox.addActionListener(togglePlainCompoundKeyAction);
        _mutableCheckBox.addActionListener(toggleBtnMutableAction);
        _proxyCheckBox.addActionListener(toggleBtnProxyAction);
        _dynamicUpdateCheckBox.addActionListener(toggleBtnDynamicUpdateAction);
        _dynamicInsertCheckBox.addActionListener(toggleBtnDynamicInsertAction);
        _selectBeforeUpdateCheckBox.addActionListener(toggleBtnSelectBeforeUpdateAction);
        _implementLifecycleCheckBox.addActionListener(toggleBtnLifecycleAction);
        _implementValidatableCheckBox.addActionListener(toggleBtnValidatableAction);
        _implementEqualsHashcode.addActionListener(toggleBtnEqualsHashcodeAction);
        _beanKeyGeneratorCombo.addActionListener(keyGeneratorComboAction);
        _beanNameField.setEditable(false);
    }

    public void setTable(Table table)
    {
        _currentTable = (HibernateTable)table;
        _implementEqualsHashcode.setBooleanNode(_currentTable.getEqualsHashcodeNode());
        _implementLifecycleCheckBox.setBooleanNode(_currentTable.getLifecycleNode());
        _implementValidatableCheckBox.setBooleanNode(_currentTable.getValidatableNode());
        _pcKeyCheckBox.setBooleanNode(_currentTable.getPlainCompoundKeyNode());
        _proxyCheckBox.setBooleanNode(_currentTable.getProxyNode());
        _mutableCheckBox.setBooleanNode(_currentTable.getMutableNode());
        _dynamicUpdateCheckBox.setBooleanNode(_currentTable.getDynamicUpdateNode());
        _dynamicInsertCheckBox.setBooleanNode(_currentTable.getDynamicInsertNode());
        _selectBeforeUpdateCheckBox.setBooleanNode(_currentTable.getSelectBeforeUpdateNode());
        _beanNameField.setText(_currentTable.getDestinationClassName());
        _schemaNameField.setText(_currentTable.getDatabaseSchema());
        _persisterField.setText(_currentTable.getPersister());
        _batchSizeField.setText(_currentTable.getBatchSize());
        _whereField.setText(_currentTable.getWhere());
        _classDescriptionField.setText(_currentTable.getClassDescription());
        _extendsField.setText(_currentTable.getExtends());
        setKeyGenerator();
        setClassScope();
        setMultipleImplements();
    }

    public Dimension getPreferredSize()
    {
        return getMinimumSize();
    }

    private void setKeyGenerator()
    {
        String aKeyGenItem[] = {
            "assigned", "native", "sequence", "identity", "increment", "hilo", "seqhilo", "uuid.hex", "uuid.string", "foreign"
        };
        String cKeyGenItem[] = {
            "assigned"
        };
        DefaultComboBoxModel cModel = null;
        if(_currentTable.isCompositeKey())
            cModel = new DefaultComboBoxModel(cKeyGenItem);
        else
            cModel = new DefaultComboBoxModel(aKeyGenItem);
        String keyGeneratorValue = _currentTable.getKeyGenerator();
        _beanKeyGeneratorCombo.setModel(cModel);
        _beanKeyGeneratorField.setEditable(!_currentTable.isCompositeKey());
        if(keyGeneratorValue.length() == 0)
        {
            _beanKeyGeneratorCombo.setSelectedItem("sequence");
            _beanKeyGeneratorField.setText(String.valueOf(String.valueOf(_currentTable.getTableElement().getName())).concat("_SEQ"));
        } else
        {
            _beanKeyGeneratorField.setText(_currentTable.getKeyGeneratorArg());
            _beanKeyGeneratorCombo.setSelectedItem(keyGeneratorValue);
        }
    }

    private void setClassScope()
    {
        _publicRadioButton.setSelected(_currentTable.getClassScope().equals("public"));
        _protectedRadioButton.setSelected(_currentTable.getClassScope().equals("protected"));
        _privateRadioButton.setSelected(_currentTable.getClassScope().equals("private"));
    }

    private void setMultipleImplements()
    {
        _implementsComboBox.removeAllItems();
        ArrayList aList = _currentTable.getImplements();
        for(int i = 0; i < aList.size(); i++)
            _implementsComboBox.addItem(aList.get(i));

    }

    private JPanel BuildTableScopePanel()
    {
        JPanel spanel = new JPanel();
        spanel.setLayout(new FlowLayout());
        ButtonGroup group = new ButtonGroup();
        group.add(_publicRadioButton);
        group.add(_protectedRadioButton);
        group.add(_privateRadioButton);
        ActionListener radioBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                _currentTable.setClassScope(e.getActionCommand());
            }

        };
        _publicRadioButton.setActionCommand("public");
        _publicRadioButton.addActionListener(radioBtnAction);
        _protectedRadioButton.addActionListener(radioBtnAction);
        _protectedRadioButton.setActionCommand("protected");
        _privateRadioButton.addActionListener(radioBtnAction);
        _privateRadioButton.setActionCommand("private");
        spanel.add(_publicRadioButton);
        spanel.add(_protectedRadioButton);
        spanel.add(_privateRadioButton);
        return spanel;
    }

    private JPanel BuildImplementsPanel()
    {
        JPanel ipanel = new JPanel();
        ipanel.setLayout(new BoxLayout(ipanel, 1));
        JButton addButton = new JButton("Add");
        JButton removeButton = new JButton("Remove");
        ActionListener addBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                String txt = _sourceTextField.getText().trim();
                if(txt.length() > 0)
                {
                    _implementsComboBox.addItem(txt);
                    _sourceTextField.setText("");
                    ArrayList aList = _currentTable.getImplements();
                    aList.add(txt);
                    _currentTable.setImplements(aList);
                }
            }

        };
        ActionListener removeBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                int idx = _implementsComboBox.getSelectedIndex();
                if(idx >= 0)
                {
                    _implementsComboBox.removeItemAt(idx);
                    ArrayList aList = _currentTable.getImplements();
                    aList.remove(idx);
                    _currentTable.setImplements(aList);
                }
            }

        };
        addButton.addActionListener(addBtnAction);
        removeButton.addActionListener(removeBtnAction);
        Dimension btnSize = new Dimension(80, 20);
        addButton.setPreferredSize(btnSize);
        removeButton.setPreferredSize(btnSize);
        Box firstRow = Box.createHorizontalBox();
        Box secondRow = Box.createHorizontalBox();
        firstRow.add(_sourceTextField);
        firstRow.add(addButton);
        secondRow.add(_implementsComboBox);
        secondRow.add(removeButton);
        ipanel.add(firstRow);
        ipanel.add(secondRow);
        return ipanel;
    }

    private JPanel BuildBatchSizePanel()
    {
        JPanel ipanel = new JPanel();
        ipanel.setLayout(new BoxLayout(ipanel, 1));
        Box firstRow = Box.createHorizontalBox();
        firstRow.add(_batchSizeLabel);
        firstRow.add(_batchSizeField);
        firstRow.add(_whereLabel);
        ipanel.add(firstRow);
        return ipanel;
    }

    private JPanel BuildWherePanel()
    {
        JPanel ipanel = new JPanel();
        ipanel.setLayout(new BoxLayout(ipanel, 1));
        Box firstRow = Box.createHorizontalBox();
        firstRow.add(_whereField);
        ipanel.add(firstRow);
        return ipanel;
    }

    private JPanel BuildKeyGeneratorPanel()
    {
        JPanel ipanel = new JPanel();
        ipanel.setLayout(new BoxLayout(ipanel, 1));
        _beanKeyGeneratorCombo.setEditable(false);
        Dimension btnSize = new Dimension(90, 20);
        _beanKeyGeneratorCombo.setPreferredSize(btnSize);
        Box firstRow = Box.createHorizontalBox();
        firstRow.add(_beanKeyGeneratorCombo);
        firstRow.add(_beanKeyGeneratorField);
        ipanel.add(firstRow);
        return ipanel;
    }

    private void updateTable(String beanName)
    {
        _currentTable.setDestinationClassName(beanName);
    }













}
